<template>
	<section>
		<!--工具条，就是高级查询的那个框-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="配置名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getQueryDepartments">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="this.pageInfo.dataList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="105">
			</el-table-column>
			<el-table-column type="index" label="序号" width="160">
			</el-table-column>
      <el-table-column prop="key" label="配置名称" width="250">
      </el-table-column>
      <el-table-column prop="value" label="系统名称" width="320">
      </el-table-column>
      <el-table-column prop="type" label="状态" min-width="80">
        <template scope="scope">
          <span v-if="scope.row.type == 0" style="color: red;">禁用</span>
          <span v-if="scope.row.type == 1" style="color:green">启用</span>
        </template>
      </el-table-column>
      <el-table-column prop="intro" label="描述" width="280">
      </el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-- 批量删除和分页 -->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
<!--			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="this.query.pageSize" :total="this.pageInfo.total" style="float:right;">
			</el-pagination>-->

<!--      layout 属性加上什么，底部就会出现什么-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="this.query.currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="10"
          style="float:right"
          layout="total,sizes, prev, pager, next,jumper"
          :total="this.pageInfo.total">
      </el-pagination>
		</el-col>


		<!--新增/修改界面-->
		<el-dialog title="新增/修改" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="配置名称" prop="key">
          <el-input v-model="saveForm.key" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="系统名称" prop="value">
          <el-input v-model="saveForm.value" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="配置描述" prop="intro">
          <el-input v-model="saveForm.intro" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.type">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" >提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'

	export default {
		data() {
			return {
				query: {  // 定义高级查询的 属性，也是要通过请求带给后端的
					keyword: '',
          currentPage:1,
          pageSize:10
				},
        pageInfo:{
          dataList: [],
          total: 0,
        },
				page: 1,
				listLoading: false,
				sels: [],//列表选中列


				saveFormVisible: false,//新增界面是否显示
				saveFormRules: {
					key: [
						{ required: true, message: '请输入配置名称', trigger: 'blur' }
					],
          value: [
            { required: true, message: '请输入系统名称', trigger: 'blur' }
          ],
				},
				//新增界面数据
				saveForm: {
          id: null,
          key: " ",
          intro: "",
          value:"",
          type: ""
        },

			}
		},
		methods: {

      // 调整每页数据条数
      handleSizeChange(val){
        this.query.pageSize=val;
        this.getConfigs()
      },
      // 点击查询按钮
      getQueryDepartments(){
        this.query.currentPage=1;
        this.getConfigs()
      },

			handleCurrentChange(val) {
				this.query.currentPage=val;
        this.getConfigs() // getConfigs 方法中的 axios 请求会自己来这里找
			},
			//获取配置列表
			getConfigs() {
				this.listLoading = true;
        // 发请求
        this.$http.put("/config",this.query)
            .then(result=>{
              result=result.data;
              if(result.success){
                this.$message({message: '查询成功',type: 'success'});
                this.listLoading = false;
                this.pageInfo =result.ajaxObj;
              }else {
                this.$message({message: '查询失败',type: 'error'});
              }
            })
            .catch(result=>{
              this.$message({message: '网络异常，查询列表失败',type: 'error'});
            })
			},


			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					this.$http.delete("/config/"+row.id)
              .then((result) => {
                result=result.data;
                if(result.success){
                  this.$message({message: '删除成功',type: 'success'});
                  this.listLoading = false;
                  this.query.currentPage=1;
                  this.getConfigs()
                }else {
                  this.$message({message: '删除失败',type: 'error'});
                }
					}).catch();
				}).catch(() => {

				});
			},
			//显示编辑界面的弹窗 --- 下拉框的回显和级联框的回显(掐头去尾，头是空字符串，尾是自己)
			handleEdit: function (index, row) {
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);   // 回显

			},
			//显示新增界面的弹窗
			handleAdd: function () {
				this.saveFormVisible = true;
        this.saveForm = {
          id: null,
          key: " ",
          intro: "",
          value:"",
          type: ""
        }
      },

			//新增的提交按钮，上级配置的级联框取数组的最后一个
			addSubmit: function () {
				this.$refs.saveForm.validate((valid) => {   // valid 就是规则的返回值，规则通过了才会有返回值
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.saveForm);  // JSON 格式的转换
              // 在发请求之前 处理级联框
              var parent =this.saveForm.parent
              if(parent && parent.length>0){
                var id=parent[parent.length-1]
                para.parent={
                  id:id
                }
              }else{
                para.parent={
                  id:null
                }
              }
              // 发请求
              this.$http.post("/config",para)
                  .then((result) => {
                    result=result.data;
                    if(result.success){
                      this.$message({message: '保存成功',type: 'success'});
                      this.addLoading=false
                      this.saveFormVisible=false
                      this.getConfigs()
                    }else {
                      this.$message({message: '保存失败',type: 'error'});
                    }
                  })
                  .catch(result => {
                    this.$message({message: '网络异常，请稍后访问',type: 'danger'});
                    console.log(result)
                  })
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          this.$http.patch("/config",ids)
              .then((result) => {
                result=result.data;
                if(result.success){
                  this.$message({message: '批量删除成功',type: 'success'});
                  this.listLoading = false;
                  this.query.currentPage=1;
                  this.getConfigs()
                }else {
                  this.$message({message: '批量删除失败',type: 'error'});
                }
              })
					    .catch(result => {

				      })
			}).catch(()=>{})
		}
    },


		mounted() {
      this.getConfigs();
    }
	}

</script>

<style scoped>

</style>